#include "UnitTest++.h"
#include "ZipCompressor.h"
#include "ZipReader.h"
#include "FileUtils.h"
#include "TestUtils.h"
#include "ZipManager.h"
#include <list>
#include <string>

TEST(ShouldFillTheFileHeaderStructureCorrectly)
{
    ZipReader reader ("resources/fileHeader.zip");
    ZipBuilder compressor;

    FilePathData data;
    data.fullPath_ = "resources/fileHeader.txt";
    data.relativePath_ = "fileHeader.txt";
    FileHeader* actualFileHeader = compressor.buildFileHeaderStructure(data);
    FileHeader* expectedFileHeader  = new FileHeader();
    TestUtils::populateFileHeader(expectedFileHeader,10,0,0,34101,17100,1604768238,18,18,14,28,"fileHeader.txt",
                       "UT//t","file header proof\n");
    CHECK(*actualFileHeader == *expectedFileHeader);

    delete expectedFileHeader;
    expectedFileHeader = 0;
    delete actualFileHeader;
    actualFileHeader = 0;
}

TEST(ShoulCompressAFileWhithoutCompressionMethod)
{
    std::vector<std::string> pathList;
    std::string path = "resources/fileToCompress.txt";
    
    const char* destinyPath = "resources";
    const char* expectedFilePath = "resources/TestCompress.zip";
    const char* fileName = "TestCompress.zip";
    pathList.push_back(path);
    
    ZipCompressor compressor;
    compressor.compress(pathList, destinyPath,fileName);

    CHECK(isFile(expectedFilePath));
    unlink(expectedFilePath);
}

TEST(ShoulCompressTwoFilesWhithoutCompressionMethod)
{
    std::vector<std::string> pathList;
    std::string fisrtFile = "resources/fileToCompress.txt";
    std::string secondFile = "resources/file1.txt";
    const char* destinyPath = "resources";
    const char* fileName = "TestCompressed.zip";
    pathList.push_back(fisrtFile);
    pathList.push_back(secondFile);
    ZipCompressor compressor;
    compressor.compress(pathList, destinyPath,fileName);
    const char* expectedFilePath = "resources/TestCompressed.zip";
    CHECK(isFile(expectedFilePath));
    unlink(expectedFilePath);
}

TEST(ShoulCompressDirectoriesWhithoutCompressionMethod)
{
    std::vector<std::string> pathList;
    std::string firstFile = "resources/TestDirectoryFileHeader";
    const char* destinyPath = "resources";
    const char* fileName = "TestCompressedDir.zip";
    pathList.push_back(firstFile);
    ZipCompressor compressor;
    compressor.compress(pathList, destinyPath,fileName);
    const char* expectedFilePath = "resources/TestCompressedDir.zip";
    CHECK(isFile(expectedFilePath));
}

TEST(ShoulCompressAndDecompressDirectoriesWhithoutCompressionMethod)
{
    createDirectory("resources/destiny");
    std::vector<std::string> pathList;
    std::string firstFile = "resources/TestCompress";
    const char* destinyPath = "resources/destiny/";
    const char* fileName = "CompressionTest.zip";
    const char* expectedFilePath = "resources/destiny/CompressionTest.zip";
    pathList.push_back(firstFile);
    ZipCompressor compressor;
    compressor.compress(pathList, destinyPath,fileName);
    CHECK(isFile(expectedFilePath));
    
    
    ZipManager manager;
    manager.openFile(expectedFilePath);
    manager.decompress(destinyPath);
    CHECK(isDirectory(firstFile.c_str()));
    system("rm -r resources/destiny");
}
